﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;


namespace WGCSFramework.images
{
    public delegate void EventGetPicture(object sender, Image im, string url, string name);
    public delegate void EventReady(object sender, EventArgs e);

    public class WgBrowserGrab
    {
        public event EventGetPicture getThumbPicture;
        public event EventReady isReady;

        public string message = "";
        public bool showScrollBars = false;
        public Image image;

        private WebBrowser browser;
        private int quality = 0;
        private int width = 1024;
        private int height = 768;
        private string url;

        public string getUrl
        {
            get
            {
                return this.url;
            }
        }

        public void grabUrl(string url)
        {
            this.grabUrl(url, this.width, this.height, this.showScrollBars);
        }

        public void grabUrl(string url, bool showScrollBars)
        {
            this.grabUrl(url, this.width, this.height, showScrollBars);
        }

        public void grabUrl(string url, int browserWidth, int browserHeight)
        {
            this.grabUrl(url, browserWidth, browserHeight, this.showScrollBars);
        }

        public void grabUrl(string url, int browserWidth, int browserHeight, bool showScrollBars)
        {
            if (browserWidth < 200 || browserHeight < 200) throw new Exception("Minimum width and height for browser is 200 (px)!");
            this.width = browserWidth;
            this.height = browserHeight;
            this.url = url;
            this.showScrollBars = showScrollBars;
            if (this.quality == 0) this.quality = 80;
            this.initBrowser();
        }

        /// <summary>
        /// Init browser, setup width & height
        /// </summary>
        private void initBrowser()
        {
            this.browser = new WebBrowser();
            this.browser.ScrollBarsEnabled = this.showScrollBars;
            this.browser.ScriptErrorsSuppressed = true;
            this.browser.WebBrowserShortcutsEnabled = false;
            this.browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
            this.browser.Width = this.width;
            this.browser.Height = this.height;
            this.browser
            this.browser.Url = new Uri(this.url);
        }

        /// <summary>
        /// Creates an image on browser DocumentCompleted event
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">e</param>
        private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string url = this.browser.Url.ToString();
            Bitmap im = new Bitmap(this.width, this.height);
            this.browser.DrawToBitmap(im, new Rectangle(this.browser.Location.X, this.browser.Location.Y, this.browser.Width, this.browser.Height));
            this.image = im;
            //this.message = url;
            this.isReady(this, EventArgs.Empty);
        }


    }
}
